home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / DRVALID.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  5KB  |  113 lines

  1. /*
  2. **  DRVALID.C
  3. */
  4.  
  5. #include <dos.h>
  6. #include <stdlib.h>
  7.  
  8. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  9. #define SUCCESS 0
  10.  
  11. #ifdef __ZTC__
  12.  #define readisk(d,n,s,b) dos_abs_disk_read(d,n,s,b)
  13. #elif defined(__TURBOC__)
  14.  #define readisk(d,n,s,b) absread(d,n,s,b)
  15. #else /* MSC or others */
  16. int readisk(int drv, int num, int start, char *buf)
  17. {
  18.       union REGS regs;
  19.       struct SREGS sregs;
  20.  
  21.       regs.h.al = (char)drv;
  22.       regs.x.cx = num;
  23.       regs.x.dx = start;
  24.       segread(&sregs);                  /* To work in any memory model  */
  25.       sregs.ds  = FP_SEG((char far *)buf);
  26.       regs.x.bx = FP_OFF((char far *)buf);
  27.       intdosx(®s, ®s, &sregs);
  28.       if (regs.x.cflag)
  29.             return ERROR;
  30.       else  return SUCCESS;
  31. }
  32. #endif
  33.  
  34. /************************************************************************/
  35. /*                                                                      */
  36. /*  drvalid()                                                           */
  37. /*                                                                      */
  38. /*  Verifies whether a disk drive is mounted in the system without      */
  39. /*  triggering the DOS critical error handler.                          */
  40. /*                                                                      */
  41. /*  Arguments: 1 - target drive (0 = A;, 1 = B:, etc.)                  */
  42. /*                                                                      */
  43. /*  Returns:   TRUE  - drive is valid                                   */
  44. /*             FALSE - drive is invalid                                 */
  45. /*             ERROR - no buffer space available                        */
  46. /*                                                                      */
  47. /*  Side effects: none                                                  */
  48. /*                                                                      */
  49. /************************************************************************/
  50.  
  51. int drvalid(int drive)
  52. {
  53.       int status;
  54.       char *buf;
  55.  
  56.       buf = (char *)malloc(4096);       /* Since we really don't know   */
  57.       if (!buf)
  58.             return ERROR;
  59.       status = readisk(drive, 1, 0, buf);
  60.       free(buf);
  61.       return(!status);
  62. }
  63.  
  64. /************************************************************************/
  65. /*                                                                      */
  66. /*  getdrv()                                                            */
  67. /*                                                                      */
  68. /*  Just as getcwd() returns the default directory, getdrv() returns    */
  69. /*  the drive letter of the current drive.                              */
  70. /*                                                                      */
  71. /*  Arguments: None.                                                    */
  72. /*                                                                      */
  73. /*  Returns:   Current drive (0 = A:, 1 = B:, etc.)                     */
  74. /*                                                                      */
  75. /*  Side effects: none                                                  */
  76. /*                                                                      */
  77. /************************************************************************/
  78.  
  79. int getdrv(void)
  80. {
  81.       union REGS regs;
  82.  
  83.       regs.h.ah = 0x19;
  84.       intdos(®s, ®s);
  85.       return (regs.h.al);
  86. }
  87.  
  88. /************************************************************************/
  89. /*                                                                      */
  90. /*  chdrv()                                                             */
  91. /*                                                                      */
  92. /*  Like chdir(), except changes drives rather than directories.        */
  93. /*                                                                      */
  94. /*  Arguments: 1 - target drive (0 = A:, 1 = B:, etc.)                  */
  95. /*                                                                      */
  96. /*  Returns: SUCCESS or ERROR                                           */
  97. /*                                                                      */
  98. /*  Side effects: none                                                  */
  99. /*                                                                      */
  100. /************************************************************************/
  101.  
  102. int chdrv(int drive)
  103. {
  104.       union REGS regs;
  105.  
  106.       if (!drvalid(drive))
  107.             return ERROR;
  108.       regs.h.ah = 0x0e;
  109.       regs.h.dl = (char)drive;
  110.       intdos(®s, ®s);
  111.       return SUCCESS;
  112. }
  113.